﻿using System;
using System.Windows.Forms;

using ProfileManager.DataAccess;

namespace ProfileManager.Forms
{
    public partial class AddServiceInfoForm : MasterForm
    {
        public Guid SelectedServiceTypeId { get; set; }

        public AddServiceInfoForm()
        {
            InitializeComponent();

            ShowCustomLeftButton = false;
            ShowCustomRightButton = false;
        }

        private void FillServiceTypeCombo()
        {
            comboServiceType.DataSource = ServiceType.Select();
            
            if (SelectedServiceTypeId != Guid.Empty)
            {
                comboServiceType.SelectedValue = SelectedServiceTypeId;
                SelectedServiceTypeId = Guid.Empty;
            }
        }

        private void AddServiceInfoForm_ClickOk(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbServiceInfoName.Text.Trim()) && comboServiceType.SelectedIndex != -1)
            {
                ServiceInfo serviceInfo = new ServiceInfo(
                    tbServiceInfoName.Text.Trim(), Guid.Parse(comboServiceType.SelectedValue.ToString()))
                    {
                        Url = tbServiceInfoUrl.Text.Trim()
                    };
                
                serviceInfo.Insert();

                if (serviceInfo.Id != Guid.Empty)
                {
                    DialogResult = DialogResult.OK;
                }
            }
        }

        private void AddServiceInfoForm_Load(object sender, EventArgs e)
        {
            FillServiceTypeCombo();
        }
    }
}